package com.example.medicalapp.activity;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.example.medicalapp.R;
import com.example.medicalapp.adapter.PrescriptionAdapter;
import com.example.medicalapp.helper.DatabaseHelper;
import com.example.medicalapp.model.Prescription;
import com.example.medicalapp.util.PreferencesUtil;

import java.util.List;

public class PatientHistoryActivity extends AppCompatActivity {
    private static final String TAG = "PatientHistoryActivity";

    private RecyclerView rvPrescriptions;
    private PrescriptionAdapter adapter;
    private TextView tvEmpty;
    private DatabaseHelper dbHelper;
    private PreferencesUtil preferencesUtil;
    private int patientId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_patient_history);
        Log.d(TAG, "onCreate called");

        // 初始化视图
        initViews();

        // 初始化数据库和偏好设置工具
        dbHelper = new DatabaseHelper(this);
        preferencesUtil = new PreferencesUtil(this);

        // 获取当前登录患者ID
        patientId = preferencesUtil.getUserId();
        Log.d(TAG, "Current patient ID: " + patientId);

        // 加载处方记录
        loadPrescriptions();
    }

    private void initViews() {
        tvEmpty = findViewById(R.id.tv_empty);
        rvPrescriptions = findViewById(R.id.rv_prescriptions);
        rvPrescriptions.setLayoutManager(new LinearLayoutManager(this));
    }

    private void loadPrescriptions() {
        // 从数据库获取当前患者的所有处方记录
        List<Prescription> prescriptions = dbHelper.getPrescriptionsByPatient(patientId);
        Log.d(TAG, "Loaded " + prescriptions.size() + " prescriptions for patient: " + patientId);

        if (prescriptions.isEmpty()) {
            // 显示空状态
            tvEmpty.setVisibility(View.VISIBLE);
            rvPrescriptions.setVisibility(View.GONE);
        } else {
            // 显示处方列表
            tvEmpty.setVisibility(View.GONE);
            rvPrescriptions.setVisibility(View.VISIBLE);

            // 设置适配器
            adapter = new PrescriptionAdapter(prescriptions, true,
                    this::onPrescriptionClick, dbHelper);
            rvPrescriptions.setAdapter(adapter);
        }
    }

    // 处方项点击事件处理
    private void onPrescriptionClick(Prescription prescription) {
        Log.d(TAG, "Prescription clicked: " + prescription.getId());

        // 显示完整的处方详情
        new AlertDialog.Builder(this)
                .setTitle("诊疗详情 - " + prescription.getCreatedDate().split(" ")[0])
                .setMessage("诊断结果：" + prescription.getFinalDiagnosis() + "\n\n" +
                        "处方：" + prescription.getFinalPrescription() + "\n\n" +
                        "AI分析：" + prescription.getAiAnalysis())
                .setPositiveButton("关闭", null)
                .show();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        // 关闭数据库连接
        if (dbHelper != null) {
            dbHelper.close();
        }
        Log.d(TAG, "onDestroy called");
    }
}